<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit">
    <a-form :form="form">
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="专业代码">
        <a-input :disabled="sou" placeholder="专业代码" v-decorator="['specialityCode', { rules: [{ required: true, message: '请填写专业代码' }] }]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="专业名称">
        <a-input placeholder="专业名称" v-decorator="['specialityName', { rules: [{ required: true, message: '请填写专业名称' }] }]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="系别">
        <a-tree-select
          v-decorator="['deptId', { rules: [{ required: true, message: '请选择系部' }] }]"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="deptTree"
          @change="onChange"
          placeholder="系别"
          treeDefaultExpandAll
        ></a-tree-select>
      </a-form-item>
      <a-form-item style="display: none;"><a-input placeholder="系别名称" v-decorator="['deptName']" /></a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveSpecialityBase, updateSpecialityBase } from '@/api/speciality'
import pick from 'lodash.pick'
export default {
  name: 'SpecialityBaseModal',
  props: {
    deptTree: {
      type: Array,
      required: true
    }
  },
  components: {},
  data () {
    return {
      deptCheck: false,
      visible: false,
      sou: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {},
  created () {},
  methods: {
    add () {
      this.form.resetFields()
      this.edit({})
      this.sou = false
    },
    edit (record) {
      this.sou = true
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'specialityCode', 'specialityName', 'deptId', 'deptName'))
      })
    },
    onChange (value, label, extra) {
      this.form.setFieldsValue({ deptName: label[0] })
      if (extra.triggerNode.$children.length > 0) {
        this.$message.error('不能选择父节点' + `${label}`)
        this.deptCheck = false
      } else {
        this.deptCheck = true
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      if (!this.deptCheck) {
        this.$message.error('不能选择父节点')
        return
      }
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          var submit = this.sou ? updateSpecialityBase : saveSpecialityBase
          submit(values)
            .then(res => {
              if (res.code === 0) {
                this.$message.success('保存成功')
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(() => {
              this.$message.error('系统错误，请稍后再试')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
